﻿using LitJson;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace TableCore.Plugin
{
    public class EnumMaskFormatter : IGenFormatter, IGenCmdInitializer
    {
        string[] mEnums;
        int[] mValues;
        bool mAutoHash;
        bool mIgnoreHasCase;

        public void Init(GTType type, Dictionary<string, string> args, string content)
        {
            if (string.IsNullOrEmpty(content))
                mEnums = new string[0];
            else
                mEnums = content.Split('\n');
            var regex = new Regex(@"(:|：)");
            string hash;
            if(args.TryGetValue("autoHash", out hash))
            {
                hash = hash.ToLower();
                mAutoHash = true;
                mIgnoreHasCase = hash == "lower" || hash == "ignore-case";
            }
            mValues = new int[mEnums.Length];
            for (int i = 0; i < mEnums.Length; i++)
            {
                var k = regex.Match(mEnums[i]);
                if (k != null && k.Success)
                {
                    var value = mEnums[i].Substring(0, k.Index).Trim();
                    if (StringUtil.StartWithIgnoreCase(value, "0x"))
                        mValues[i] = int.Parse(value.Substring(2), System.Globalization.NumberStyles.HexNumber);
                    else
                        mValues[i] = int.Parse(value);
                    mEnums[i] = mEnums[i].Substring(k.Index + k.Length).Trim();
                }
                else
                {
                    mEnums[i] = mEnums[i].Trim();
                    mValues[i] = i > 0 ? (1 << (i - 1)) : 0;
                }
            }
            if (type.CaseType == ECaseType.lower)
            {
                for (int i = 0; i < mEnums.Length; i++)
                {
                    mEnums[i] = mEnums[i].ToLower();
                }
            }
            else if (type.CaseType == ECaseType.upper)
            {
                for (int i = 0; i < mEnums.Length; i++)
                {
                    mEnums[i] = mEnums[i].ToUpper();
                }
            }
        }

        public bool IsValid(string input)
        {
            return true;
        }

        int GetEnumValue(string enumname)
        {
            if (string.IsNullOrEmpty(enumname))
                return 0;
            string str = enumname.ToLower().Trim();
            for (int i = 0; i < mEnums.Length; i++)
            {
                if (mEnums[i] == str)
                    return mValues[i];
            }

            return 0;
        }

        public JsonData Format(GTFile.Content content, string input, string cellName, string comment)
        {
            int n = 0;
            if (input == "[all]")
            {
                for (int i = 0; i < mValues.Length; i++)
                {
                    n |= mValues[i];
                }
            }
            else
            {
                if (int.TryParse(input, out n))
                    return n;
                string[] args = input.Split(',');
                for (int i = 0; i < args.Length; i++)
                {
                    n |= GetEnumValue(args[i]);
                }
            }
            return n;
        }

        public IExportData ExportData(GTFile.Content content, string input, string cellName, string comment)
        {
            return null;
        }
    }
}
